﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 
/// GUIまわりを管理するクラス
/// 
/// </summary>

public class GUIManager : SingletonMonoBehaviour<GUIManager>
{
	
	//[SerializeField]
	//private GUIPanel[] panels;
	private GUIPanel[] panels;

	#region PrivateValues
	private Dictionary<System.Type, GUIPanel> panelDic;
	private List<GUIPanelBase> OpenGUIPanelStack = new List<GUIPanelBase>(); // 開いているパネルへの参照
	#endregion


	//==================================================
	protected override void Awake ()
	{
		base.Awake ();

		GetPanels();
		InitPanels ();
		InitDic();
	}

	//==================================================

	/**
	 * GUIPanelを探して集める
	 */
	void GetPanels()
	{
		panels = FindObjectsOfType<GUIPanel>();
	}

	/**
	 * パネル初期化
	 */
	void InitPanels ()
	{
		foreach (var tmp in panels) {
			tmp.gameObject.transform.localPosition = Vector3.zero;
			tmp.gameObject.SetActive (false);
		}

	}

	/**
	 * パネル辞書初期化
	 */
	void InitDic()
	{
		panelDic = new Dictionary<System.Type, GUIPanel>();

		foreach (var tmp in panels)
		{
			panelDic.Add(tmp.GetType(), tmp);

		}
	}
	//==================================================


	/**
	 * パネルを開く
	 */
	public void Open<T>() where T : GUIPanel
	{
		if (panelDic.ContainsKey(typeof(T)))
		{
			var tmp = panelDic[typeof(T)];
			tmp.gameObject.SetActive(true);
			tmp.OnOpen();
			OpenGUIPanelStack.Add(tmp);
		}
		else {
			Debug.LogError ("Panel Not Found : " + typeof(T).Name);
		}
	}


	//==================================================

	/**
	 * パネルを閉じる
	 */
	public void Close (GUIPanel _panel)
	{
		if (panelDic.ContainsKey(_panel.GetType())) {
			var tmp = panelDic[_panel.GetType()];

			if (tmp.gameObject.activeSelf == false) {
				Debug.LogError("オープンしていないパネルをクローズしています :" + _panel.name);
			}

			tmp.OnClose ();
			tmp.gameObject.SetActive (false);

			OpenGUIPanelStack.Remove (tmp);

		}
		else {
			Debug.LogError ("Panel Not Found : " + _panel.name);
		}
	}

	/**
	 * パネルを閉じる
	 */
	public void Close<T>() where T : GUIPanel
	{
		if (panelDic.ContainsKey(typeof(T)))
		{
			var tmp = panelDic[typeof(T)];

			if (tmp.gameObject.activeSelf == false) {
				Debug.LogError("オープンしていないパネルをクローズしています :" + tmp.name);
			}

			tmp.OnClose();
			tmp.gameObject.SetActive(false);
			OpenGUIPanelStack.Remove(tmp);

		}
		else {
			Debug.LogError ("Panel Not Found : " + typeof(T).Name);
		}
	}



	
	//==================================================
	
	/**
	 * パネルを取得
	 */
	public T GetPanel<T>() where T : GUIPanel
	{
		if (panelDic.ContainsKey(typeof(T)))
		{
			var tmp = panelDic[typeof(T)];

			return (T)tmp;
		}
		else {
			Debug.LogError ("Panel Not Found : " + typeof(T).Name);
			return null;
		}
	}


	//==============================================


	/**
	 * update
	 */
	void Update()
	{
		// Only Android
		#if UNITY_ANDROID

		if (Input.GetKeyDown (KeyCode.Escape)) {
			OnBackKeyDown();
		}
		#endif
	}

	/**
	 * バックキーが押された時の処理
	 */
	private void OnBackKeyDown()
	{
		// 最後に開いたパネルかポップアップに通知
		OpenGUIPanelStack [OpenGUIPanelStack.Count - 1].OnBackKeyDown ();
	}



}